<?php

namespace App\Models;

class ShopWaterBillCategory extends BaseModel
{
    protected $table = "shop_waterbill_category";


    protected $rules = [
        'admin_id' => 'required',
        'shop_id' => 'required|unique:shop_waterbill_category,shop_id',
        'category_data' => 'required',
    ];

    protected $message = [
        'admin_id.required' => '获取账户信息失败',
        'shop_id.required' => '获取店铺信息失败',
        'shop_id.unique' => '该店铺分类信息已存在，请勿重复添加',
        'category_data.required' => '获取分类信息失败',
    ];

    protected $casts = [
        'category_data' => 'array',
    ];


    public function addOrUpdate()
    {
        $data['admin_id'] = is_login();
        $data['shop_id'] = request('shop_id');
        $ids = request('ids');
        if (!(!empty($ids) && is_array($ids))) {
            $this->error = '请选择要操作的数据';
            return false;
        }
        $where['id'] = ['in', $ids];
        $where['admin_id'] = is_login();
        $category = new WaterBillCategory();
        $cates = $category->_getList($where, 'id,title,buy_num,gift_num,total_num,description,sort,status')->toArray();
        if (!empty($cates)) {
            $data['category_data'] = $cates;
        }
        if (request('id')) {
            $data['id'] = trim(request('id'));
            $this->rules['shop_id'] .= ',' . $data['id']; //唯一字段验证,在更新时排除当前id
            $old = explode(',', request('old'));
            $arr = array_diff($old, $ids);
//            if (!empty($arr)) {
//                foreach ($arr as $v) {
//                    if (in_array($v,$old)) {
//                        $_res = $this->checkData($arr);
//                        if (!$_res) {
//                            $this->error = '套餐下有数据无法删除';
//                            return false;
//                        }
//                    }
//                }
//            }
        }
        if (!$this->_validate($data)) {
            return false;
        }
        $res = $this->_save($data);
        if ($res) {
            $id = isset($data['id']) ? $data['id'] : $this->id;
            //记录行为
//            action_log('update_shop', 'shop', $id);
        }
        return $res;
    }

    public function checkData($ids)
    {
        if (!$ids) {
            $this->error = '参数错误';
            return false;
        }

        if (!is_array($ids)) {
            $ids = [$ids];
        }
        $waterbill = new WaterBill();
        $where['id'] = ['in', $ids];
        $where['shop_id'] = request('shop_id');
        $res = $waterbill->_getOne($where);
        if ($res) {
            return false;
        }
        return true;
    }

    public function del()
    {
        $ids = request('ids');
        if (!$ids) {
            $this->error = '参数错误';
            return false;
        }
        if (!is_array($ids)) {
            $ids = [$ids];
        }
        $res = $this->checkData($ids);
        if ($res) {
            $this->error = '分类下有数据无法删除';
            return false;
        }
        $res = $this->_getOne(['shop_id' => request('shop_id')])->toArray();
        if (!empty($res['category_data'])) {
            foreach ($res['category_data'] as $k => $v) {
                foreach ($ids as $m) {
                    if ($m == $v['id']) {
                        unset($res['category_data'][$k]);
                    }
                }
            }
        }
        $res = $this->_save($res);
        if (!$res) {
            $this->error = '删除失败';
            return false;
        }
        return true;
    }

    public function getShopWaterBillCategory($sid, $where = [])
    {
        $categoryData = $this->_getOne(['shop_id' => $sid], '*');
        $tree = [];
        if($categoryData){
            $category_data = array_column($categoryData->toArray()['category_data'],'id');
            $where['id'] = ['in', $category_data];
            $tree = (new WaterBillCategory())->_getList($where)->toArray();
        }

        return $tree;
    }

    public function getShopBillCategory($shopId)
    {
        $shopCategory = $this->_getOne(['shop_id' => $shopId], 'category_data');
        $billCategory = $shopCategory['category_data'];
        $categories = (new WaterBillCategory())->_getList(['admin_id' => plat_admin_id(),'status' => 1],'id')->toArray();
        $ids  = array_column($categories,'id');
        $result = [];
        foreach($billCategory as $v) {
            if(in_array($v['id'], $ids)) {
                array_push($result, $v);
            }
        }

        return $result;
    }


}
